# TODO 导入ActionChians类
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from selenium.webdriver.common.by import By
import time


class Mouse:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://sahitest.com/demo/')

    def click(self):
        # 点击进入到click测试页面
        self.driver.find_element(By.LINK_TEXT, 'Clicks Page').click()
        btn = self.driver.find_element(By.XPATH, '/html/body/form/input[3]')
        # 创建ActionChains对象，导入driver值
        ac = ActionChains(driver=self.driver)
        # 使用ac对象调用click方法.perform方法执行
        ac.click(btn).perform()

    def context_click(self):
        self.driver.get('https://sahitest.com/demo/clicks.htm')
        # 定位到右键按钮
        btn = self.driver.find_element(By.XPATH,'/html/body/form/input[4]')
        # 创建ac对象
        ac = ActionChains(driver=self.driver)
        # 使用右键方法，传入标签，执行方法
        ac.context_click(btn).perform()

    def double_click(self):
        self.driver.get('https://sahitest.com/demo/clicks.htm')
        btn = self.driver.find_element(By.XPATH,'/html/body/form/input[2]')
        ac = ActionChains(driver=self.driver)
        # 双击鼠标
        ac.double_click(btn).perform()

    def drag_and_drop(self):
        self.driver.get('https://sahitest.com/demo/dragDropMooTools.htm')
        ac = ActionChains(driver=self.driver)
        # 源位置
        source = self.driver.find_element(By.ID,'dragger')
        # 目标位置
        target = self.driver.find_element(By.XPATH,'/html/body/div[2]')
        # 拖动操作
        ac.drag_and_drop(source,target).perform()

    def move_to_element(self):
        self.driver.get('https://sahitest.com/demo/mouseover.htm')
        ac = ActionChains(driver=self.driver)
        # 定位到需要悬停的标签
        div = self.driver.find_element(By.XPATH,'/html/body/div/div')
        # 悬停操作
        ac.move_to_element(div).perform()

    def click_and_hold(self):
        # 请求本地页面，准备好绝对路径地址
        url = "file:///C:/Users/%E6%9D%8E%E8%8A%B8/Desktop/f/%E4%BD%9C%E4%B8%9A/code/automated_testing/day3/html/mouse_hold.html"
        self.driver.get(url)
        btn = self.driver.find_element(By.ID,'btn1')
        ac = ActionChains(driver=self.driver)
        # 长按左键操作
        ac.click_and_hold(btn).perform()

    def quit(self):
        time.sleep(2)
        self.driver.quit()


if __name__ == '__main__':
    ms = Mouse()
    # ms.click()
    # ms.context_click()
    # ms.double_click()
    ms.drag_and_drop()
    # ms.move_to_element()
    # ms.click_and_hold()
    ms.quit()
